﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ConsoleApp
{
    public class demo_modify_config_file
    {
        public static void demo()
        {
            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            int localVersion = Convert.ToInt32(config.AppSettings.Settings["localVersion"].Value);
            Console.WriteLine(string.Format("ori {0}", localVersion));

            config.AppSettings.Settings.Remove("localVersion");
            config.AppSettings.Settings.Add("localVersion", (localVersion + 1).ToString());

            config.Save();
            //ConfigurationManager.RefreshSection("appSettings"); //here we just new a instance,if not,it will cache,so we need to refreshsection
            var configa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var localVersiona = config.AppSettings.Settings["localVersion"].Value;
            Console.WriteLine(string.Format("after save {0}", localVersiona));
        }
    }
}
